package com.hnkywl.jip.service;

import com.hnkywl.jip.dao.InvoiceMergeDao;
import com.hnkywl.jip.dao.InvoiceVerifyDao;
import com.hnkywl.jip.interceptor.AuthInterceptor;
import com.hnkywl.jip.utils.VoucherItemData;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-2-25
 * Time: 下午8:46
 * To change this template use File | Settings | File Templates.
 */
@Service
public class InvoiceMergeService {
    private InvoiceMergeDao invoiceMergeDao;

    /**
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param companyId 公司ID
     * @param operator  操作人
     * @return
     */
    @Transactional
    public Page<Object[]> findInvoiceMerge(Date startTime, Date endTime, Integer status,
                                           Integer companyId, Integer voucher, String operator, Integer limit, Integer start) {
        return invoiceMergeDao.findInvoiceMerge(startTime, endTime, status, companyId, voucher, operator, limit, start);
    }

    @Transactional
    public Map getVoucher(Integer id, String uuid ,List<VoucherItemData> voucherItemDataList) {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Object[]> list = invoiceMergeDao.getVoucher(id);
        //凭证分录信息
        List<Map> items = new ArrayList<Map>();
        int order = 1;
        for (Object[] item : list) {
            Map<String, Object> voucherItem = new HashMap<String, Object>();
            voucherItem.put("id", order);//唯一标识
            voucherItem.put("vchrid", uuid);//凭证主表ID
            voucherItem.put("itemorder", order);//分录编号
            voucherItem.put("subjectcode", item[1] != null ? item[1] : "");//科目代码
            voucherItem.put("subjectname", item[2] != null ? item[2] : "");//科目名称
            String debit = null;
            if (voucherItemDataList != null)
                debit = voucherItemDataList.get(order - 1).getDebit();
            if (debit != null && !debit.isEmpty())
                voucherItem.put("debit", debit);
            else
                voucherItem.put("debit", item[3] != null ? item[3].toString() : "");//借方金额
            String credit = null;
            if (voucherItemDataList != null)
                credit = voucherItemDataList.get(order - 1).getCredit();
            if (credit != null && !credit.isEmpty())
                voucherItem.put("credit", credit);
            else
                voucherItem.put("credit", item[4] != null ? item[4].toString() : "");//贷方金额
            voucherItem.put("qty", "0");//数量
            voucherItem.put("price", "0");//单价
            voucherItem.put("currencycode", "CNY");//货币代码
            voucherItem.put("exchrate", "1.000000");//汇率
            if (debit != null && !debit.isEmpty())
                voucherItem.put("orgnd", debit);
            else
                voucherItem.put("orgnd", item[3] != null ? item[3].toString() : "");//借方原币
            if (credit != null && !credit.isEmpty())
                voucherItem.put("orgnc", credit);
            else
                voucherItem.put("orgnc", item[4] != null ? item[4].toString() : "");//贷方原币
            voucherItem.put("settlementcode", "");//结算方式代码
            voucherItem.put("settlementno", "");//结算号
            voucherItem.put("bizdate", item[12] != null ? item[12] : "");//业务日期
            String digest = null;
            if (voucherItemDataList != null)
                digest = voucherItemDataList.get(order - 1).getDigest();
            if (digest != null && !digest.isEmpty())
                voucherItem.put("digest", digest);//摘要
            else
                voucherItem.put("digest", item[0] != null ? item[0] : "");//摘要
            voucherItem.put("bizno", "");//业务号
            voucherItem.put("suppliercode", item[8] != null ? item[8] : "");//供应商代码
            String departmentcode = null;
            if (voucherItemDataList != null)
                departmentcode = voucherItemDataList.get(order - 1).getDeptId();
            if (departmentcode != null && !departmentcode.isEmpty())
                voucherItem.put("departmentcode", departmentcode);
            else
                voucherItem.put("departmentcode", "");//部门代码
            voucherItem.put("customercode", item[7] != null ? item[7] : "");//客户代码
            voucherItem.put("staffcode", "");//职员/代码
            voucherItem.put("isdept", item[10] != null ? item[10] : "");
            voucherItem.put("isadd", item[9] != null ? item[9] : "");
            voucherItem.put("ismc", item[6] != null ? item[6] : "");
            voucherItem.put("ismd", item[5] != null ? item[5] : "");
            items.add(voucherItem);
            order++;
        }
        result.put("items", items);
        return result;
    }

    @Transactional
    public Map getRevokeInfo(Integer id) {
        Map<String, Object> revokeInfo = new HashMap<String, Object>();
        List result = invoiceMergeDao.getRevokeInfo(id);
        if (result == null) {
            throw new RuntimeException("没该公司的对照信息!");
        }
        revokeInfo.put("id", result.get(0));//凭证ID
        revokeInfo.put("vchrid", result.get(1));//凭证ID
        revokeInfo.put("unitcode", result.get(2));//单位代码
        revokeInfo.put("acctyear", result.get(3));//会计年度
        return revokeInfo;
    }

    @Transactional
    public Map getVoucherMaster(Integer id, Integer attachment, String uuid, String year, String mouth, Map userInfo) {
        Map<String, Object> voucherMaster = new HashMap<String, Object>();
        List result = invoiceMergeDao.getVoucherMaster(id);
        if (result == null) {
            throw new RuntimeException("没该公司的对照信息!");
        }
        voucherMaster.put("id", uuid);
        voucherMaster.put("vchrtypecode", result.get(0));//凭证类别
        voucherMaster.put("acctyear", year != null ? year : "");//会计年度
        voucherMaster.put("acctperiod", mouth != null ? mouth : "");//会计期间
        voucherMaster.put("createdate", new Date());//凭证日期
        voucherMaster.put("unitcode", result.get(2));//组织机构代码
        voucherMaster.put("acctbookcode", "01");//账薄标识
        voucherMaster.put("createuser", userInfo.get(AuthInterceptor.USER_NAME_SESSION_KEY));//制单人
        voucherMaster.put("attachment", attachment);//附件张数
        voucherMaster.put("memo", "");//备注
        return voucherMaster;
    }

    @Transactional
    public List getInvoiceMerge(Integer id) {
        return invoiceMergeDao.getInvoiceMerge(id);
    }

    @Autowired
    public void setInvoiceMergeDao(InvoiceMergeDao invoiceMergeDao) {
        this.invoiceMergeDao = invoiceMergeDao;
    }
}
